//
//  DLExportExcelModel.m
//  DontRem
//
//  Created by apple on 2021/3/22.
//

#import "DLExportExcelModel.h"
#import <xlsxwriter.h>
@interface DLExportExcelModel()
@property(nonatomic,copy)NSString * fileName;
@end
@implementation DLExportExcelModel
+(NSString*)getFilePath{
    return [self getFileNamePath];
}
- (void)setCloumnArr:(NSArray<NSString *> *)cloumnArr{
    _cloumnArr = cloumnArr;
}
- (void)setCloumnWidthArr:(NSArray<NSNumber *> *)cloumnWidthArr{
    _cloumnWidthArr = cloumnWidthArr;
}
- (void)setDataArr:(NSArray<NSDictionary *> *)dataArr{
    _dataArr = dataArr;
}
- (void)setCloumnKeyArr:(NSArray<NSString *> *)cloumnKeyArr{
    _cloumnKeyArr = cloumnKeyArr;
}
-(NSString*)createFile{
    // 创建新xlsx文件，路径需要转成c字符串
    lxw_workbook  *workbook  = workbook_new([self.filePath UTF8String]);
    // 创建sheet
    lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
    //设置列宽
    if (self.cloumnWidthArr && self.cloumnWidthArr.count > 0) {
        for (int i = 0; i<self.cloumnWidthArr.count; i++) {
            NSInteger wid = [self.cloumnWidthArr[i] integerValue];
            worksheet_set_column(worksheet, i, i+1, wid , NULL);
        }
    }else{
        for (int i = 0; i<self.cloumnArr.count; i++) {
            worksheet_set_column(worksheet, i, i+1, 30 , NULL);
        }
    }
    lxw_format *format = workbook_add_format(workbook);
    format_set_bold(format);// 加粗
    format_set_font_color(format, LXW_COLOR_BLACK);//颜色
    format_set_align(format, LXW_ALIGN_CENTER);// 水平居中
    format_set_align(format, LXW_ALIGN_VERTICAL_CENTER);//垂直居中
    format_set_border(format, LXW_BORDER_MEDIUM);
    //创建头部key   ex： |商家|联系方式|地址|
    for (int i =0; i<self.cloumnArr.count; i++) {
        NSString * cloumnName = self.cloumnArr[i];
        //第二个参数：行 第三个参数：列
        worksheet_write_string(worksheet, 0, i, (char*)[cloumnName UTF8String], format);
    }
    //创建每一行的内容
    for (int i =0;i<self.dataArr.count;i++) {
        NSDictionary * dic = self.dataArr[i];
        lxw_format *format = workbook_add_format(workbook);
//        format_set_font_size(format, 5);//字体大小
        format_set_font_color(format, LXW_COLOR_BLACK);//颜色
        format_set_align(format, LXW_ALIGN_CENTER);// 水平居中
        format_set_align(format, LXW_ALIGN_VERTICAL_CENTER);//垂直居中
        format_set_border(format, LXW_BORDER_MEDIUM);// 边框（四周）：中宽边框

        for (int j =0; j< self.cloumnKeyArr.count; j++) {
            NSString * key = self.cloumnKeyArr[j];
            id obj = dic[key];
            char * val;
            if ([obj isKindOfClass:[NSNumber class]]) {
                NSString * num = ((NSNumber*)obj).stringValue;
                val = (char*)[num UTF8String];
            }else if([obj isKindOfClass:[NSString class]]){
                val = (char*)[((NSString*)obj) UTF8String];
                
            }else{
                return @"";
            }
            worksheet_write_string(worksheet, i+1, j, val, format);
        }
    }
    workbook_close(workbook);
    return self.filePath;
}
+(NSString* )getFileNamePath{
    NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    NSString * name = @"DontRem.xlsx";
    NSString *filename = [documentPath stringByAppendingPathComponent:name];
    return filename;
}
- (NSString *)filePath {
    return [DLExportExcelModel getFileNamePath];
}
@end
